﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAL;

namespace CTRL
{
    public sealed class CtrlBatteryModel
    {
        private static volatile CtrlBatteryModel instance;
        private static object syncRoot = new Object();

        private IDALBatteryModel bModelRepo;

        public CtrlBatteryModel()
        {
            this.bModelRepo = new DALBatteryModel(new CarBatteryEntities());
        }

        public static CtrlBatteryModel Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new CtrlBatteryModel();
                    }
                }
                return instance;
            }
        }

        public IEnumerable<BatteryModel> GetAllBatteryModels()
        {
            return bModelRepo.GetBatteryModels();
        }

        public BatteryModel GetBatteryModel(int bModelID)
        {
            return bModelRepo.GetBatteryModelByID(bModelID);
        }

        public BatteryModel GetBatteryModelByName(string bModelName)
        {
            return bModelRepo.GetBatteryModelByName(bModelName);
        }

        public int CreateBatteryModel(string _model, double _capacity, string _type, int _manufacturerID)
        {
            CtrlBatteryManufacturer manufactCtrl = CtrlBatteryManufacturer.Instance;

            if (manufactCtrl.GetBatteryManufacturer(_manufacturerID) != null)
            {
                BatteryModel bModel = new BatteryModel()
                {
                    model = _model,
                    capacity = _capacity,
                    type = _type,
                    manufacturerID = _manufacturerID
                };
                bModelRepo.InsertBatteryModel(bModel);
            }

            return bModelRepo.Save();
        }

        public int UpdateBatteryModel(int modelID, string _model, double _capacity, string _type, int _manufacturerID)
        {
            CtrlBatteryManufacturer manufactCtrl = CtrlBatteryManufacturer.Instance;

            BatteryModel bModel = GetBatteryModel(modelID);

            if ((manufactCtrl.GetBatteryManufacturer(_manufacturerID) != null) && (bModel != null))
            {
                bModel.model = _model;
                bModel.capacity = _capacity;
                bModel.type = _type;
                bModel.manufacturerID = _manufacturerID;
                bModelRepo.UpdateBatteryModel(bModel);
            }

            return bModelRepo.Save();
        }

        public int DeleteBatteryModel(int modelID)
        {
            BatteryModel bModel = GetBatteryModel(modelID);

            if (bModel != null)
                bModelRepo.DeleteBatteryModel(modelID);

            return bModelRepo.Save();
        }
    }
}